package com.lagou.client;

import com.lagou.model.Student;
import com.lagou.model.User;

import java.io.IOException;
import java.util.ArrayList;

public class StudentView {

    private ClientInitClose cic;

    public StudentView(ClientInitClose cic){
        this.cic = cic;
    }

    /**
     * 构造方法显示管理员对学员的操作界面
     */
    void studentView() throws IOException, ClassNotFoundException {
        boolean flag = true;
        while (flag) {
            System.out.println("-----------------");
            System.out.println("  欢迎进入学员管理");
            System.out.println("-----------------");
            System.out.println("[1] 增加学员 \t");
            System.out.println("[2] 删除学员 \t");
            System.out.println("[3] 修改学员 \t");
            System.out.println("[4] 查找学员 \t");
            System.out.println("[5] 返回上一级 \t");

            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    addStudents();
                    break;
                case 2:
                    deleStudents();
                    break;
                case 3:
                    changeStudents();
                    break;
                case 4:
                    searchStudents();
                case 5:
                    cic.getOos().writeObject("exit");
                    ClientView cv = new ClientView(null, null);
                    cv.clientShow();
                    flag=false;
                default:
                    System.out.println("输入错误");
            }
        }
    }

    private void addStudents() throws IOException {
        cic.getOos().writeObject("addStudents");
        System.out.println("请输入要添加的学生的学号"); String s = ClientScanner.getScanner().next();
        System.out.println("请输入要添加的学生的姓名"); String s1 = ClientScanner.getScanner().next();
        System.out.println("请输入要添加的学生的年龄"); int s2 = ClientScanner.getScanner().nextInt();
        Student stu = new Student(s,s1,s2);
        //添加student的对象到list中
        cic.getOos().writeObject(stu);
        System.out.println("添加新的学员发送到服务器了");

    }

    private void deleStudents() throws IOException, ClassNotFoundException {
        cic.getOos().writeObject("deleteStudents");
       // Student stu1 = new Student();
        ArrayList<Student> removeArray;
        removeArray = (ArrayList<Student>) cic.getOis().readObject();
        for (Student u: removeArray){
            System.out.println(u);
        }
        System.out.println("请输入删除的账户");
        String name2 =ClientScanner.getScanner().next();
        cic.getOos().writeObject(name2);

    }

    private void changeStudents() throws IOException, ClassNotFoundException {
        cic.getOos().writeObject("change");
        ArrayList<User> remarry;
        remarry = (ArrayList<User>) cic.getOis().readObject();
        for (User u:remarry){
            System.out.println(u);
        }
        System.out.println("请输入修改后的账户和班级");
        String name1 =ClientScanner.getScanner().next();
        int class2 =ClientScanner.getScanner().nextInt();
        User u2= new User(name1,class2);
        cic.getOos().writeObject(u2);
    }

    private void searchStudents() throws IOException, ClassNotFoundException {
        cic.getOos().writeObject("search");
        System.out.println("请输入查找的账户");
        String name3 =ClientScanner.getScanner().next();
        cic.getOos().writeObject(name3);
        User u5=(User)cic.getOis().readObject();
        if (u5==null){
            System.out.println("没有该用户");
        }else {
            System.out.println("账户：" + u5.getUserName() + "，班级:" + u5.getClassroom());
        }
    }
}
